<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Dt_layout {
    protected $title;
    protected $metas = array();
    protected $scripts=array();
    protected $css = array();
    protected $layout='default/layout';
    protected $blocks = array();
    protected $CI;
    public $data_view= array();
    public function get_title() {
        return $this->title;
    }

    public function __construct() {
        $this->CI = & get_instance();
    }
    
    public function set_title($title) {
        $this->title = $title;
    }

    public function get_metas() {
        return $this->metas;
    }

    public function get_scripts() { //print_r($this->scripts);
        return $this->scripts;
    }

    public function get_css() {
        return $this->css;
    } 
    public function get_layout() {
        return $this->layout;
    }

    public function set_layout($layout) {
        $this->layout = $layout;
    }

    public function get_blocks() {
        return $this->blocks;
    }
    
    public function set_blocks(array $blocks) {
        $this->blocks = $blocks;
    }
    
   
    public function add_scripts($url){
         if(is_array($url)){
             foreach ($url as $val) {
                if(strpos($val,'http://')||  strpos($val,'https://')){
                    $this->scripts[] = $val;
                }else {
                    $this->scripts[] = base_url().$val;
                }
             }
         }else{
            if(strpos($url,'http://')||  strpos($url,'https://')){
                $this->scripts[] = $url;
            }else {
                $this->scripts[] =  base_url().$url;
            }
         }
    }
    
    public function add_styles($url){
         if(is_array($url)){
             foreach ($url as $val) {
                if(strpos($val,'http://')||  strpos($val,'https://')){
                    $this->css[] = $val;
                }else {
                    $this->css[] = base_url().$val;
                }
             }
         }else {
            if(strpos($url,'http://')||  strpos($url,'https://')){
                $this->css[] = $url;
            }else {
                $this->css[] = base_url().$url;;
            }
         }
    }
    
    public function add_metas($metaArr){
        reset($metaArr);
        $first = current($metaArr);
        if(is_array($first)){
             foreach ($metaArr as $val) {
                 $this->metas[] = $val;
             }
        }else {
                $this->metas[] = $metaArr;
        }
    }
    
    /**data must format
     * @input array(
     *      'menu'=> ...,
     * )
     */
    public function set_data($data){
        foreach ($data as $key => $val) {
            $this->data_view[$key] = $val;
        }
    }
    
    public function render($module_view){
        if($this->layout=='')  throw new Exception('You must set layou name by setLayout function');
        $this->data_view['module_view'] = $module_view;
        $this->data_view['the_title'] = $this->get_title();
        $this->data_view['the_metas'] = $this->get_metas();
        $this->data_view['the_scripts'] = $this->get_scripts();
        $this->CI->load->view($this->layout,$this->data_view);
    }
    
}
?>
